package com.itheima.mall.web.sms;

import cn.hutool.core.convert.Convert;
import com.itheima.mall.pojo.sms.HomeNewProduct;
import com.itheima.mall.pojo.sms.HomeRecommendProduct;
import com.itheima.mall.pojo.sms.HomeRecommendSubject;
import com.itheima.mall.pojo.sms.SmsHomeBrand;
import com.itheima.mall.service.sms.HomeNewProductService;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("home")
@Slf4j
public class HomeNewProductController {
    @Autowired
    private HomeNewProductService homeNewProductService;

    /**
     * 添加首页新品
     * @param list
     * @return
     */
    @PostMapping("/newProduct/create")
    public R newProductCreate(@RequestBody List<Map> list){
        homeNewProductService.newProductCreate(list);
        return R.succeed(null,"添加成功");
    }

    /**
     * 批量删除首页新品
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/newProduct/delete")
    public R newProductDelete(Long[] ids){
        Boolean result = homeNewProductService.newProductDelete(ids);
        return result?R.succeed(null,"删除成功"):R.error("商品正在推荐,不能删除");
    }

    /**
     * 分页查询首页新品
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/newProduct/list")
    public R newProductList(Integer pageNum,Integer pageSize,String productName,Integer recommendStatus){
        PageResult<HomeNewProduct> pageResult = homeNewProductService.newProductList(pageNum,pageSize,productName,recommendStatus);
        return R.succeed(pageResult,null);
    }

    /**
     * 批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/newProduct/update/recommendStatus")
    public R newProductUpdateRecommendStatus(Long[] ids,@RequestParam("recommendStatus") Integer recommendStatus){
        System.err.println(ids);
        homeNewProductService.newProductUpdateRecommendStatus(ids,recommendStatus);
        return R.succeed(null,"更新成功");
    }

    /**
     * 更新状态
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/newProduct/update/sort/{id}")
    public R newProductUpdateSortById(@PathVariable("id") Long id,Integer sort){
        homeNewProductService.newProductUpdateSortById(id,sort);
        return R.succeed(null,"更新成功");
    }

    /**
     * 添加首页推荐
     * @param list
     * @return
     */
    @PostMapping("/recommendProduct/create")
    public R recommendProductCreate(@RequestBody List<Map> list){
        homeNewProductService.recommendProductCreate(list);
        return R.succeed(null,"添加成功");
    }

    /**
     * 批量删除首页推荐
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/recommendProduct/delete")
    public R recommendProductDelete(Long[] ids){
        Boolean result = homeNewProductService.recommendProductDelete(ids);
        return result?R.succeed(null,"删除成功"):R.error("商品正在推荐,不能删除");
    }

    /**
     * 分页查询首页推荐
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/recommendProduct/list")
    public R recommendProductList(Integer pageNum,Integer pageSize,String productName,Integer recommendStatus){
        PageResult<HomeRecommendProduct> pageResult = homeNewProductService.recommendProductList(pageNum,pageSize,productName,recommendStatus);
        return R.succeed(pageResult,null);
    }

    /**
     * 批量修改首页推荐商品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/recommendProduct/update/recommendStatus")
    public R recommendProductUpdateRecommendStatus(Long[] ids,@RequestParam("recommendStatus") Integer recommendStatus){
        homeNewProductService.recommendProductUpdateRecommendStatus(ids,recommendStatus);
        return R.succeed(null,"更新成功");
    }

    /**
     * 更新状态
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/recommendProduct/update/sort/{id}")
    public R recommendProductUpdateSortById(@PathVariable("id") Long id,Integer sort){
        homeNewProductService.recommendProductUpdateSortById(id,sort);
        return R.succeed(null,"更新成功");
    }

    /**
     * 添加首页推荐专题
     * @param list
     * @return
     */
    @PostMapping("/recommendSubject/create")
    public R recommendSubjectCreate(@RequestBody List<Map> list){
        homeNewProductService.recommendSubjectCreate(list);
        return R.succeed(null,"添加成功");
    }

    /**
     * 批量删除首页专题
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/recommendSubject/delete")
    public R recommendSubjectDelete(Long[] ids){
        Boolean result = homeNewProductService.recommendSubjectDelete(ids);
        return result?R.succeed(null,"删除成功"):R.error("专题正在推荐,不能删除");
    }

    /**
     * 分页查询首页推荐专题
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    @GetMapping("/recommendSubject/list")
    public R recommendSubjectList(Integer pageNum,Integer pageSize,String subjectName,Integer recommendStatus){
        PageResult<HomeRecommendSubject> pageResult = homeNewProductService.recommendSubjectList(pageNum,pageSize,subjectName,recommendStatus);
        return R.succeed(pageResult,null);
    }

    /**
     * 批量修改首页专题状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/recommendSubject/update/recommendStatus")
    public R recommendSubjectUpdateRecommendStatus(Long[] ids,@RequestParam("recommendStatus") Integer recommendStatus){
        homeNewProductService.recommendSunjectUpdateRecommendStatus(ids,recommendStatus);
        return R.succeed(null,"更新成功");
    }

    /**
     * 更新状态
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/recommendSubject/update/sort/{id}")
    public R recommendSubjectUpdateSortById(@PathVariable("id") Long id,Integer sort){
        homeNewProductService.recommendSubjectUpdateSortById(id,sort);
        return R.succeed(null,"更新成功");
    }

    /**
     * 添加推荐品牌
     * @param
     * @return
     */
    @PostMapping("/brand/create")
    public R createBrand(@RequestBody Map map){
        SmsHomeBrand homeBrand=new SmsHomeBrand();
        homeBrand.setBrandId(Convert.toLong(49));
        homeBrand.setRecommendStatus(1);
        homeBrand.setBrandName("海澜之家");
        homeBrand.setSort(10);
        homeNewProductService.createBrand(homeBrand);
        return R.succeed(null,"添加成功");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/brand/delete")
    public R deleteBrand(@RequestParam("ids") int[] ids){
        Boolean brand = homeNewProductService.deleteBrand(ids);
        return brand?R.succeed(null,"删除成功"):R.error("有关联");
    }
    @GetMapping("/brand/list")
    public R<PageResult<SmsHomeBrand>> getList(@RequestParam(value = "brandName",required = false)String brandName,
                                               @RequestParam("pageNum")Integer pageNum,
                                               @RequestParam("pageSize")Integer pageSize,
                                               @RequestParam(value = "recommendStatus",required = false)Integer recommendStatus){
        PageResult<SmsHomeBrand> list = homeNewProductService.getList(brandName, pageNum, pageSize, recommendStatus);
        return R.succeed(list,"查询成功");
    }
    @PostMapping("/brand/update/recommendStatus")
    public R updateStatus(@RequestParam("recommendStatus")Integer recommendStatus,
                          @RequestParam("ids")int[] ids){
        homeNewProductService.updateStatus(recommendStatus,ids);
        return R.succeed(null,"修改成功");
    }
 @PostMapping("brand/update/sort/{id}")
    public R updateSort(@PathVariable Integer id,@RequestParam("sort")Integer sort){
        homeNewProductService.updateSort(id,sort);
        return R.succeed(null,"修改成功");
 }
}
